let house_id = '' // 房屋的id
let house_index = -1 // 房屋的下标
Page({
  data: {
    dialogVisible: false,
    houseList: [],
    empty: false, // 默认房屋列表不是空的
  },
  onShow() {
    this.getHouseList()
  },
  async getHouseList() {
    const { code, data: houseList } = await wx.http.get('/room')
    if (code !== 10000) return wx.utils.toast()
    this.setData({
      houseList,
      empty: houseList.length === 0, // 空的
    })
  },
  // 删除房屋
  async deleteHouse() {
    const { code } = await wx.http.delete(`/room/${house_id}`)
    if (code !== 10000) return wx.utils.toast()
    // 数组的splice方法删除
    this.data.houseList.splice(house_index, 1)
    this.setData({
      houseList: this.data.houseList,
      empty: this.data.houseList.length === 0,
    })
  },
  dialogClose(ev) {
    ev.detail === 'confirm' && this.deleteHouse()
  },
  swipeClose(ev) {
    const { position, instance } = ev.detail
    // 点击删除...
    if (position === 'right') {
      // 显示 Dialog 对话框
      this.setData({
        dialogVisible: true,
      })
      house_id = ev.mark.id
      house_index = ev.mark.index
      // swiper-cell 滑块关闭
      instance.close()
    }
  },

  goDetail(ev) {
    wx.navigateTo({
      url: '/house_pkg/pages/detail/index?id=' + ev.mark.id,
    })
  },

  addHouse() {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index',
    })
  },
})
